<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//path
require(CONFIG_PATH . CONFIG_DIR_LIB . 'Smarty-2.6.14/libs/Smarty.class.php');

//
define('TPL_BASE', CONFIG_PATH . CONFIG_DIR_TEMPLATES .'base/');
define('TPL_CURRENT', CONFIG_PATH . CONFIG_DIR_TEMPLATES . CONFIG_THEME .'/');
define('TPL_CURRENT_C', CONFIG_PATH . CONFIG_DIR_CACHE . 'tpl/' . CONFIG_THEME . '/');
define('TPL_CURRENT_CONFIG', CONFIG_PATH . CONFIG_DIR_SETTINGS . 'tpl/' . CONFIG_THEME . '/');
define('TPL_CACHE', CONFIG_PATH . CONFIG_DIR_CACHE . 'pagecache/');
define('TPL_CACHE_TIME', 3600);

//
if( !is_dir(TPL_CURRENT_C) || !file_exists(TPL_CURRENT_C) ) {
 	if( !@mkdir(TPL_CURRENT_C) ) {
		die("Cannot create directory: " . TPL_CURRENT_C);
	}
}

// Template Class
class Template extends Smarty {
 	function Template() {
		$this->template_base_dir = TPL_BASE;
        $this->template_dir = TPL_CURRENT;
        $this->compile_dir = TPL_CURRENT_C;
        $this->config_dir = TPL_CURRENT_CONFIG;
        $this->cache_dir = TPL_CACHE;
		
		//$this->caching = true;
		//$this->use_sub_dirs = true;
		$this->cache_sub_dirs = true;
		$this->cache_dir_base = '';
		//
		$this->cache_lifetime = 3600;
		$this->_file_perms = 0777;
		$this->_dir_perms = 0777;
    }
	
	function setCaching($caching = true) {
		if( $caching == false ) {
			$this->caching = false;
		} else {
			$this->caching = true;
		}
	}
	
	function isCaching() {
		return $this->caching;
	}
	
	function setCacheID($key = '') {
		if( $key == '' ) {
			$key = SYS_RT_REQUEST_URI;
		}
		$key .= '_' . (($_SESSION['suser']->userid > 0) ? 1 : 0);
		$this->cache_id = $this->_get_hash_key($key);
	}
	
	function getCacheID($key = '') {
		if( $key == '' ) {
			return $this->cache_id;
		} else {
			$key .= '_' . (($_SESSION['suser']->userid > 0) ? 1 : 0);
			return $this->_get_hash_key($key);
		}
	}
	
	function setTplFile($tpl_file) {
		$this->tpl_file = $tpl_file;
	}
	
	function getTplFile() {
		return $this->tpl_file;
	}
	
	function isCached($tpl_file = '', $cache_id = '') {
		if( $tpl_file == '' ) {
			$tpl_file = $this->tpl_file;
		}
		if( $cache_id == '' ) {
			$cache_id = $this->cache_id;
		}
		if( (SYS_RT_CURRENT_TIME - $this->tsGet()) < TPL_CACHE_TIME ) {
			$this->setCaching(false);
			if( strlen($tpl_file) > 0 && strlen($cache_id) > 0 ) {
				if( $this->cache_sub_dirs == true ) {
					if( $this->cache_dir_base == '' ) {
						$this->cache_dir_base = $this->cache_dir;
						$this->cache_dir = $this->cache_dir_base . $this->_get_sub_dirs($cache_id) . '/';
						if( !is_dir($this->cache_dir) ) {
							@mkdir($this->cache_dir);
						}
					}
					$this->clear_cache($tpl_file, $cache_id);
				} else {
					$this->clear_cache($tpl_file, $cache_id);
				}
			}
			
			return false;
		}
		if( $this->caching == true && strlen($tpl_file) > 0 && strlen($cache_id) > 0 ) {
			if( $this->cache_sub_dirs == true ) {
				if( $this->cache_dir_base == '' ) {
					$this->cache_dir_base = $this->cache_dir;
					$this->cache_dir = $this->cache_dir_base . $this->_get_sub_dirs($cache_id) . '/';
					if( !is_dir($this->cache_dir) ) {
						@mkdir($this->cache_dir);
					}
				}
				return $this->is_cached($tpl_file, $cache_id);
			} else {
				return $this->is_cached($tpl_file, $cache_id);
			}
		}
		
		return false;
	}
	
	function checkCached($tpl_file, $cache_id = '', $caching = true) {
		if( $cache_id == '' ) {
			$cache_id = SYS_RT_REQUEST_URI;
		}
		$this->setCaching($caching);
		$this->setTplFile($tpl_file);
		$this->setCacheID($cache_id);
		if( $this->isCached() == false ) {
			return true;
		}
		
		return false;
	}
	
	function _get_hash_key($key) {
		return abs(crc32($key)) . '_' . md5($key);
		//return abs(crc32(ereg_replace("[^A-Za-z0-9\-\_]", '_', $key)));
	}
	
	function _get_sub_dirs($cache_id) {
		return substr(abs(crc32(ereg_replace("[^A-Za-z0-9\-\_]", '_', $cache_id))), 0, 3);

	}
	
	function tsUpdate() {
		$timestamptfile = CONFIG_PATH . CONFIG_DIR_CACHE . 'timestamp.txt';
		$fp = fopen($timestamptfile, 'w');
		fwrite($fp, SYS_RT_CURRENT_TIME);
		fclose($fp);
		
		//
		//unset($_SESSION['_ts']);
		$_SESSION['_ts'] = SYS_RT_CURRENT_TIME;
		//
		global $settingObj;
		$settingObj->updateItemB('lastupdate', SYS_RT_CURRENT_TIME);
		$settingObj->cacheclean();
	}
	
	function tsGet() {
		$ts = 0;
		
		global $SETTINGS;
		if( isset($SETTINGS['lastupdate']) && $SETTINGS['lastupdate'] + TPL_CACHE_TIME > SYS_RT_CURRENT_TIME ) {
			$ts = $SETTINGS['lastupdate'];
		} elseif( isset($_SESSION['_ts']) ) {
			if( isset($SETTINGS['lastupdate']) && $SETTINGS['lastupdate'] > $_SESSION['_ts'] ) {
				$ts = $SETTINGS['lastupdate'];
			} else {
				$ts = $_SESSION['_ts'];
			}
		} elseif( isset($SETTINGS['lastupdate']) && $SETTINGS['lastupdate'] > TPL_CACHE_TIME ) {
			$ts = $SETTINGS['lastupdate'];
		} else {
			$timestamptfile = CONFIG_PATH . CONFIG_DIR_CACHE . 'timestamp.txt';
			$fp = fopen($timestamptfile, 'r');
			if( $fp ) {
				$ts = file_get_contents($timestamptfile);
				fclose($fp);
				
				$ts = substr($ts, 0, 10);
			}
		}
		if( !isset($_SESSION['_ts']) || (isset($_SESSION['_ts']) && $ts > $_SESSION['_ts']) ) {
			$_SESSION['_ts'] = $ts;
		}
		
		return $ts;
	}
	
	function use_sub_dirs($use_sub_dirs = true) {
		if( $use_sub_dirs == true ) {
			$this->use_sub_dirs = true;
		} else {
			$this->use_sub_dirs = false;
		}
	}
	
	//
	function get_vars_context() {
		$args = func_get_args();
		if( sizeof($args) > 0 ) {
			$d = $this->_tpl_vars['CONTEXT'];
			foreach($args as $arg) {
				if( isset($d[$arg]) ) {
					$d = $d[$arg];
				} else {
					return NULL;
				}
			}
			return $d;
		} else {
			if( isset($this->_tpl_vars['CONTEXT']) ) {
				return $this->_tpl_vars['CONTEXT'];
			}
		}
		
		return NULL;
	}
	
	function set_vars_context($key, $value) {
		$this->_tpl_vars['CONTEXT'][$key] = $value;
	}
	
	function get_vars_content() {
		$args = func_get_args();
		if( sizeof($args) > 0 ) {
			$d = $this->_tpl_vars['CONTENT'];
			foreach($args as $arg) {
				if( isset($d[$arg]) ) {
					$d = $d[$arg];
				} else {
					return NULL;
				}
			}
			return $d;
		} else {
			if( isset($this->_tpl_vars['CONTENT']) ) {
				return $this->_tpl_vars['CONTENT'];
			}
		}
		
		return NULL;
	}
	
	function set_vars_content($key, $value) {
		$this->_tpl_vars['CONTENT'][$key] = $value;
	}
	
	function get_vars_settings() {
		$args = func_get_args();
		if( sizeof($args) > 0 ) {
			$d = $this->_tpl_vars['SETTINGS'];
			foreach($args as $arg) {
				if( isset($d[$arg]) ) {
					$d = $d[$arg];
				} else {
					return NULL;
				}
			}
			return $d;
		} else {
			if( isset($this->_tpl_vars['SETTINGS']) ) {
				return $this->_tpl_vars['SETTINGS'];
			}
		}
		
		return NULL;
	}
	
	function set_vars_settings($key, $value) {
		$this->_tpl_vars['SETTINGS'][$key] = $value;
	}
}
//
?>